﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TruongVu.Business;
using TruongVu.Data;

namespace TruongVuWebsite.Admin
{
    public partial class Customer : System.Web.UI.Page
    {
        private CustomerModel _customerModel = new CustomerModel();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                return;
            }

            if (Request["ID"] != null)
            {
                int id;
                if (int.TryParse(Request["ID"], out id))
                {
                    TruongVu.Data.Customer customer = this._customerModel.Get(id);
                    if (customer != null)
                    {
                        this.NameTextBox.Text = customer.Name;
                        this.AddressTextBox.Text = customer.Address;
                        this.PhoneTextBox.Text = customer.Phone;
                        this.WebsiteTextBox.Text = customer.Website;
                    }
                }
            }
        }

        protected void SubmitButton_Click(object sender, ImageClickEventArgs e)
        {
            TruongVu.Data.Customer customer = new TruongVu.Data.Customer();
            if (Request["ID"] != null)
            {
                int id = -1;
                if (int.TryParse(Request["ID"], out id))
                {
                    customer.ID = id;
                }
            }

            customer.Name = this.NameTextBox.Text;
            customer.Address = this.AddressTextBox.Text;
            customer.Phone = this.PhoneTextBox.Text;
            customer.Website = this.WebsiteTextBox.Text;

            if (Request["ID"] != null)
            {
                this._customerModel.Update(customer);
            }
            else
            {
                this._customerModel.Insert(customer);
            }

            Response.Redirect("CustomerManagement.aspx");
        }
    }
}